#include <sourcemod>
#include <sdktools>
#include "dd_client_validation.sp"
#undef REQUIRE_PLUGIN

public Plugin:myinfo = 
{
	name = "[REVOLUT10N-Doomsday] Tank Mod",
	author = "Monkey Drone",
	description = "Tank Mod for 16 Player Coop",
	version = "1.0",
	url = "http://www.piratemelons.com"
};

public OnPluginStart()
{
	HookEvent("tank_spawn",Tank_Spawn);
	HookEvent("player_death", _UMUL4D2_Event_Player_Death);
}
static SurvCount = 0;
static cTanks = 0;
static Handle:tankHpDisplayer = INVALID_HANDLE;
static sCountTank = 0;
static const tankHp = 2000;
static finalHpTank = 0;
static Tank_Colors[5][3]={{255, 0, 0},{106, 90, 205},{46, 139, 87},{255, 215, 0},{255, 130, 071}};
/* Tank Colors in Order, Red, Slate Blue, Sea Green, gold, Sienna */
static color_Chooser = 0;
static Float:TankPos[3];
static NoOfTanks = 0;
static NoOneTankID = 0;
static NoTwoTankID = 0;



public Action:Tank_Spawn(Handle:event, String:event_name[], bool:dontBroadcast)
{
	SurvCount=PlayerCounter();
	//PrintToChatAll("Number of Players is %i", SurvCount);
	if (SurvCount >= 1)
	{
		//PrintToChatAll("Spawning Second Tank Now");
		CountTheTanks();
		GetTankLocation();
		if (CTheTanks() == 1)
			{
				SpawnSecondTank();
				SetTankHp();
			}
		cTTRTimeRes();
	}

	new client = GetClientOfUserId(GetEventInt(event, "userid"));
	if(!client || client > L4D2_MAXPLAYERS)
		{
			return;
		}
		else
			{
				if (IsPlayerTank(client))
				{
					Beautify_Tank(client);	
				}
			}
	_UmUL4D2_Tank_HP_Dp_Creator();
}

//This function counts number of tanks on the map.
public CTheTanks(){
	for (new i_client=1; i_client <= GetMaxClients(); i_client++)
	{
		if (IsClientInGame(i_client) && IsPlayerAlive(i_client) && (IsFakeClient(i_client)) && (GetClientTeam(i_client) == TEAM_INFECTED) && (IsPlayerTank(i_client)))
		{
			if (IsPlayerTank(i_client))
				{
					cTanks++;	
				}
		}
	}
	CreateTimer(5.0, RstCTank);
	return cTanks;
}

//Resets Tank Counter Variable to Zero
public Action:RstCTank(Handle:Timer)
	{
		cTanks = 0;
	}
	
	
//------------------------------Start of Tank HP Display Code--------------------------------------------//


stock bool:IsValidInf(client){
    
    if(!IsClientInGame(client)) return false;
        
    if(GetClientTeam(client) != TEAM_INFECTED) return false;
        
    return true;
} 

//Initializes a repeatable time to display tank HP after every 30 seconds, Don't know if we will keep this on 10v10  
public Action:_UmUL4D2_Tank_HP_Dp_Creator(){
	if (tankHpDisplayer == INVALID_HANDLE){
	tankHpDisplayer = CreateTimer(30.0, DisplayTankHpLeft, _, TIMER_REPEAT);
	}
}

//The Tank Timer Function to Display HP
public Action:DisplayTankHpLeft(Handle:Timer){
	for (new i_client=1; i_client <= GetMaxClients(); i_client++)
					{
						if (IsClientInGame(i_client) && IsPlayerAlive(i_client) && (IsFakeClient(i_client)) && (GetClientTeam(i_client) == TEAM_INFECTED) && (IsPlayerTank(i_client)))
						{
							if (IsPlayerTank(i_client))
								{
									PrintToChatAll("\x01[\x02Tank HP Left\x01] \x03= \x04%i", GetEntProp(i_client, Prop_Send, "m_iHealth"));
								}
						}
					}
}

public Action:_UMUL4D2_Event_Player_Death(Handle:event, const String:name[], bool:dontBroadcast){
	
	new tCountHpDp = 0;
	for (new i_client=1; i_client <= GetMaxClients(); i_client++)
			{
				if (IsClientInGame(i_client) && IsPlayerAlive(i_client) && (IsFakeClient(i_client)) && (GetClientTeam(i_client) == TEAM_INFECTED) && (IsPlayerTank(i_client)))
				{
					if (IsPlayerTank(i_client))
						{
							tCountHpDp++;	
						}
				}
			}	
	
	if (tCountHpDp == 0)
		{
			new client = GetClientOfUserId(GetEventInt(event, "userid"));
			
			if(!client || client > L4D2_MAXPLAYERS || !IsValidInf(client)) return Plugin_Continue;
			
			decl String:class[100];
			GetClientModel(client, class, sizeof(class));
			
			if(StrContains(class, "tank", false) != -1) {
				if(tankHpDisplayer != INVALID_HANDLE)
					{ 
						KillTimer(tankHpDisplayer);
						tankHpDisplayer = INVALID_HANDLE;
					}
				
			}  
			
		}
	return Plugin_Continue;
}
//------------------------------End of Tank HP Display Code----------------------------------------------//

//------------------------------Start of Tank HP Adjuster Code--------------------------------------------//

public SetTankHp()
	{	
		for (new i_client=1; i_client <= GetMaxClients(); i_client++)
					{
						if (IsClientInGame(i_client) && IsPlayerAlive(i_client) && (GetClientTeam(i_client) == TEAM_INFECTED) && (IsPlayerTank(i_client)))
						{
							if (IsPlayerTank(i_client))
								{
									finalHpTank = 9000;
									SetEntProp(i_client, Prop_Send, "m_iHealth", finalHpTank);
									PrintToChatAll("\x01[\x02Tank Spawned\x01] \x03Tank HP is \x04%i", GetEntProp(i_client, Prop_Send, "m_iHealth"));
								}
						}
					}
	}
//------------------------------End of Tank HP Adjuster Code----------------------------------------------//

//------------------------------Start of Tank Multiple Color Code-----------------------------------------//

public Action:Beautify_Tank(tankNum){
	/*SetEntProp(entity, Prop_Send, "m_nRenderMode", mode, 1);*/
	SetEntityRenderMode(tankNum, RENDER_TRANSCOLOR);
//	SetEntityRenderColor(tankNum, Tank_Colors[0], Tank_Colors[1], Tank_Colors[2], 255);
//}

	if (color_Chooser >= 0 && color_Chooser <= 4){
		SetEntityRenderColor(tankNum, Tank_Colors[color_Chooser][0], Tank_Colors[color_Chooser][1], Tank_Colors[color_Chooser][2], 255);
		color_Chooser++;
	}
	if (color_Chooser > 4 || color_Chooser < 0)
				{
					color_Chooser = 0;
				}	
}

//------------------------------End of Tank Multiple Color Code-------------------------------------------//

//------------------------------Start of Second Tank Spawner Code-------------------------------------------//

public SpawnSecondTank(){
	new GiveFlags = GetCommandFlags("z_spawn");
	SetCommandFlags("z_spawn", GiveFlags & ~FCVAR_CHEAT);
	new clientx=GetAnySurvivor();
	FakeClientCommand(clientx, "z_spawn tank");
	SetCommandFlags("z_spawn", GiveFlags | FCVAR_CHEAT);
	TeleportEntity(NoTwoTankID, TankPos, NULL_VECTOR, NULL_VECTOR);
	//PrintToChatAll("Teleporting Tank of ID %i ", NoTwoTankID);
}

public cTTRTimeRes(){
	CreateTimer(5.0, cTTReset);
}
public Action:cTTReset(Handle:Timer)
	{
		NoOneTankID = 0;
		NoTwoTankID = 0;
		NoOfTanks = 0;
	}

public GetAnySurvivor(){

for(new client = 1; client <= L4D2_MAXPLAYERS; client++){

if(IsValidAliveSurv(client)) return client;

}

return 0;

}

public GetTankLocation(){
	if (NoOfTanks == 1)
		{
			for (new i_client=1; i_client <= GetMaxClients(); i_client++)
			{
				if (IsClientInGame(i_client) && IsPlayerAlive(i_client) && (IsFakeClient(i_client)) && (GetClientTeam(i_client) == TEAM_INFECTED) && (IsPlayerTank(i_client)))
				{
					if (IsPlayerTank(i_client))
						{
							GetClientAbsOrigin(i_client, TankPos);
							//PrintToChatAll("Setting First tank ID");
							NoOneTankID = i_client;
							//PrintToChatAll("First Tank ID is %i", NoOneTankID);
						}
				}
			}		
		}
	if (NoOfTanks == 2)
		{
			for (new i_client=1; i_client <= GetMaxClients(); i_client++)
			{
				if (IsClientInGame(i_client) && IsPlayerAlive(i_client) && (IsFakeClient(i_client)) && (GetClientTeam(i_client) == TEAM_INFECTED) && (IsPlayerTank(i_client)))
				{
					if (IsPlayerTank(i_client))
						{
							if (NoOneTankID != i_client)
								{
									NoTwoTankID = i_client;
									//PrintToChatAll("Second Tank ID is %i", NoTwoTankID);
								}
						}
				}
			}			
		}
	NoOfTanks = 0;
}

public CountTheTanks(){
	for (new i_client=1; i_client <= GetMaxClients(); i_client++)
			{
				if (IsClientInGame(i_client) && IsPlayerAlive(i_client) && (IsFakeClient(i_client)) && (GetClientTeam(i_client) == TEAM_INFECTED) && (IsPlayerTank(i_client)))
				{
					if (IsPlayerTank(i_client))
						{
							NoOfTanks++;	
						}
				}
			}	
}

//------------------------------End of Second Tank Spawner Code-------------------------------------------//